package app;
import java.awt.*;
import javax.swing.*;

import javax.swing.event.DocumentListener;

import java.awt.event.*;


public class CurrencyConverterView extends JFrame {
	private static final long serialVersionUID = 
		-2179332027583486600L;
	final double INITIAL_NUMBER = -1;
	CurrencyConverterModel model;
	JLabel  jlabTitle, jlabDollarAmount, jlabEquivalent;
	JButton jbtnCompute, jbtnClear, jbtnQuit;
	JRadioButton jrbtnBrazil, jrbtnCanada, jrbtnEU, jrbtnJapan;
	static final int TEXT_FIELD_COLUMN = 10;
	JTextField txtFldequivAmountIn, txtFldUSDollarAmount;
	JLabel jlabPlaceHolder;
	ButtonGroup rbtnGrp = new ButtonGroup();

	public CurrencyConverterView(CurrencyConverterModel model) {
		this.model = model;
		inializeComponents();
		
	}
	void setQuitActionListner(ActionListener al ){
		jbtnQuit.addActionListener(al);
	}
	void setComputeActionListner(ActionListener al ){
		jbtnCompute.addActionListener(al);
	}
	void setClearActionListner(ActionListener al ){
		jbtnClear.addActionListener(al);
	}
	void setBrazilSelectedActionListner(ActionListener al ){
		jrbtnBrazil.addActionListener(al);
		
	}
	void setCanadaSelectedActionListner(ActionListener al ){
		
		jrbtnCanada.addActionListener(al);
	
		
	}
	void setEUSelectedActionListner(ActionListener al ){
		
		jrbtnEU.addActionListener(al);
		
		
	}
	void setJapanSelectedActionListner(ActionListener al ){
		
		jrbtnJapan.addActionListener(al);
		
	}
	void setUSDChangedActionListner(DocumentListener kl){
		txtFldUSDollarAmount.getDocument().addDocumentListener(kl);
	}


	
	
	private void inializeComponents() {
		getContentPane().setLayout(new FlowLayout());
		setTitle("Currency Converter");
		setSize(400,300); 	  // default size is 0,0
		setLocation(400,200); // default is 0,0 (top left corner)

		// Terminate the program when the user closes the application
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//create the first panel and add it to the frame 						 (2)
		JPanel jpnl1 = new JPanel();
		// set the preferred size of the first panel  
		jpnl1.setPreferredSize((new Dimension(400, 30)));
		jpnl1.setOpaque(true);   // make the panel opaque
		jpnl1.setBorder(BorderFactory.createEmptyBorder());


		//create the second panel for conversion and add it to the frame
		JPanel jpnl2 = new JPanel(new GridLayout(3,3));
		// set the preferred size of the first panel  
		jpnl2.setPreferredSize((new Dimension(300,50)));
		jpnl2.setOpaque(true);
		//jpnl2.setBorder(BorderFactory.createLineBorder(Color.BLUE));


		//create the third panel for currency selection and add it to the frame
		JPanel jpnl3 = new JPanel(new GridLayout(4,1));
		// set the preferred size of the first panel  
		jpnl3.setPreferredSize((new Dimension(200,160)));
		jpnl3.setOpaque(true);
		jpnl3.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Choose Conversion Type"));


		// create the fourth panel for buttons and add it to the frame
		JPanel jpnl4 = new JPanel(new GridLayout(3,1));
		// set the preferred size of the first panel  
		jpnl4.setPreferredSize((new Dimension(100,80)));
		jpnl4.setOpaque(true);

		// create the components & add them to the Panel                        (3)
		jlabTitle = new JLabel("Currency Converter");
		jpnl1.add(jlabTitle);

		// Label and text fields for U.S. Dollar Amount
		jlabDollarAmount = new JLabel("U.S. Dollar Amount: ");
		txtFldUSDollarAmount = new JTextField(TEXT_FIELD_COLUMN);
			//new JFormattedTextField(NumberFormat.getIntegerInstance()); 
			
		jlabDollarAmount.setLabelFor(txtFldUSDollarAmount);

		// Label and text fields for Equivalent In...
		jlabEquivalent   = new JLabel("Equivalent in... ");
		jlabPlaceHolder  = new JLabel("");
		txtFldequivAmountIn    = new JTextField(TEXT_FIELD_COLUMN);
		//	new JFormattedTextField(NumberFormat.getIntegerInstance());
		
		//jlabEquivalent.setLabelFor(equivAmountIn);

		//jpnl2.add(jlabEquivalent, BorderLayout.WEST);
		//jpnl2.add(jlabEquivalent, BorderLayout.CENTER);
		jpnl2.add(jlabDollarAmount);
		jpnl2.add(txtFldUSDollarAmount);
		jpnl2.add(jlabEquivalent);
		jpnl2.add(txtFldequivAmountIn);
		jpnl2.add(jlabPlaceHolder);

		// RadioButtons now		
		jrbtnBrazil = new JRadioButton("Brazil", false);
		jrbtnCanada = new JRadioButton("Canada", false);
		jrbtnEU     = new JRadioButton("European Community", false);
		jrbtnJapan  = new JRadioButton("Japan", false);

		// add reaio buttons to a group
		
		rbtnGrp.add(jrbtnBrazil);
		rbtnGrp.add(jrbtnCanada);
		rbtnGrp.add(jrbtnEU);
		rbtnGrp.add(jrbtnJapan);
		jpnl3.add(jrbtnBrazil);
		jpnl3.add(jrbtnCanada);
		jpnl3.add(jrbtnEU);
		jpnl3.add(jrbtnJapan);

		// Computation, Clear & Quit Buttons now
		jbtnCompute = new JButton("Compute");
		jbtnClear   = new JButton("Clear");
		jbtnQuit    = new JButton("Quit");

		ButtonGroup btnGrp = new ButtonGroup();
		btnGrp.add(jbtnCompute);
		btnGrp.add(jbtnClear);
		btnGrp.add(jbtnQuit);

		jpnl4.add(jbtnCompute);
		jpnl4.add(jbtnClear);
		jpnl4.add(jbtnQuit);

		// Add the panels to the frame
		getContentPane().add(jpnl1);
		getContentPane().add(jpnl2);
		getContentPane().add(jpnl3);
		getContentPane().add(jpnl4);
		
	}



	// Getters for the Radio Buttons
	public JRadioButton getJrbtnBrazil() {
		return jrbtnBrazil;
	}

	public JRadioButton getJrbtnCanada() {
		return jrbtnCanada;
	}

	public JRadioButton getJrbtnEU() {
		return jrbtnEU;
	}

	public JRadioButton getJrbtnJapan() {
		return jrbtnJapan;
	}
	
	// Getters for the Compute, Clear & Quit Buttons
	public JButton getJbtnCompute() {
		return jbtnCompute;
	}

	public JButton getJbtnClear() {
		return jbtnClear;
	}

	public JButton getJbtnQuit() {
		return jbtnQuit;
	}
	
	// Textfield getters
	public JTextField getTxtFldequivAmountIn() {
		return txtFldequivAmountIn;
	}
	
	public JTextField getTxtFldUSDollarAmount() {
		return txtFldUSDollarAmount;
	}
	public void displayResult() {
		double result = model.getResult();
//		if(result == INITIAL_NUMBER) return;
		txtFldequivAmountIn.setText(String.valueOf(result));
		jlabPlaceHolder.setText(model.getMsg());
		
	}
	public void clear() {
		txtFldUSDollarAmount.setText("");
		txtFldequivAmountIn.setText("");
		jlabPlaceHolder.setText("");
		rbtnGrp.clearSelection();
		
	}
	public String getValue() {
		
		// TODO Auto-generated method stub
		return txtFldUSDollarAmount.getText();
	}	
	
	
}